package com.zhu.salesmanagement;

import java.util.ArrayList;
import java.util.List;

import com.dms2.salesmanagement.R;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.utils.DialogUtil;

import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ProductsListActivity extends Activity implements AdapterView.OnItemClickListener{

	private final static int C_LIST_QUERY_TOKEN = 0;
	private final static int C2_LIST_QUERY_TOKEN = 1;

	private ListView mListView = null;
	private ListQueryHandler mListQueryHandler = null;
	private List<ItemInfo> mList = new ArrayList<ItemInfo>();;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_listview);

        //mListView = (ListView)findViewById(R.id.listview);
        mListView.setOnItemClickListener(this);
        mListQueryHandler = new ListQueryHandler(this.getContentResolver());

        startSync();
    }

	public void onResume() {
        super.onResume();
        Actions.setAutoTime(this);
    }

	private void startSync(){
		mListQueryHandler.startQuery(C_LIST_QUERY_TOKEN, null, SalesSettings.ProductCategoryColumns.CONTENT_URI, 
				null, null, null, null);
	}

	private void intiCustomerList(Cursor cursor){
		mList.clear();
		try {
            final int idIndex = cursor.getColumnIndexOrThrow(SalesSettings.ProductCategoryColumns._ID);
            final int storeidIndex = cursor.getColumnIndexOrThrow(SalesSettings.ProductCategoryColumns.PRODUCTCATEGORY_ID);
            final int nameIndex = cursor.getColumnIndexOrThrow(SalesSettings.ProductCategoryColumns.PRODUCTCATEGORY_NAME);

            while (cursor.moveToNext()) {
            	ItemInfo item = new ItemInfo();
            	item.id = cursor.getString(storeidIndex);
            	item.name = cursor.getString(nameIndex);
            	mList.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateStoreInfoList error");
        } finally {
            cursor.close();
        }
		ItemInfo item = new ItemInfo();
		item.id = "------------------------------------------";
		item.name = "下面是所有产品";
		mList.add(item);
		//mListView.setAdapter(new MyImgAdapter());
		mListQueryHandler.startQuery(C2_LIST_QUERY_TOKEN, null, SalesSettings.ProductsColumns.CONTENT_URI, 
				null, null, null, null);
	}
	
	
	private void intiProductList(Cursor cursor){
		try {
            final int idIndex = cursor.getColumnIndexOrThrow(SalesSettings.ProductsColumns._ID);
            final int storeidIndex = cursor.getColumnIndexOrThrow(SalesSettings.ProductsColumns.PRODUCT_ID);
            final int nameIndex = cursor.getColumnIndexOrThrow(SalesSettings.ProductsColumns.PRODUCT_NAME);

            while (cursor.moveToNext()) {
            	ItemInfo item = new ItemInfo();
            	item.id = cursor.getString(storeidIndex);
            	item.name = cursor.getString(nameIndex);
            	mList.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("zhu", "updateStoreInfoList error");
        } finally {
            cursor.close();
        }

		mListView.setAdapter(new MyImgAdapter());
	}

	private final class ListQueryHandler extends AsyncQueryHandler {
        public ListQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            switch (token) {
                case C_LIST_QUERY_TOKEN:
                    intiCustomerList(cursor);
                    break;
                case C2_LIST_QUERY_TOKEN:
                	intiProductList(cursor);
                	break;
            }
        }

        @Override
        protected void onDeleteComplete(int token, Object cookie, int result) {

        }
    }

	public final class ItemInfo{
		public String id ;
		public String name;
	}


	public final class ViewHolder{
		public TextView text1;
		public TextView text2;
	}

	
	class MyImgAdapter extends BaseAdapter {

		public MyImgAdapter() {
			super();
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			if (convertView == null) {
				convertView = LayoutInflater.from(ProductsListActivity.this)
						.inflate(R.layout.layout_listviewitem, null);
				holder = new ViewHolder();
				holder.text1 = (TextView) convertView.findViewById(R.id.text1);
				holder.text2 = (TextView) convertView.findViewById(R.id.text2);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.text1.setText(mList.get(position).name);
			holder.text2.setText(mList.get(position).id);
			return convertView;
		}

	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
//		CustomerItem visitTaskInfo = mList.get(arg2);
//		String content = visitTaskInfo.sCustomerID +"\n" + visitTaskInfo.sLatitude + "\n" +visitTaskInfo.sLongitude +"\n" +
//				visitTaskInfo.iVisitCategory;
//		DialogUtil.getSimpleDialog(ProductsListActivity.this,
//				visitTaskInfo.sCustomerName,
//				content, null).show();
	}
}
